#include "loadable.h"
#include <xml/IFactoryEnvironment.h>
#include <xml/IFactoryRecord.h>
#include <xml/IXmlNode.h>
#include <log/Log.h>
#include <scene/ISceneNodeLoadable.h>
#include <xml/utils/base_scenenode.h>

class LoadableFactoryRecord:public irr::xml::IFactoryRecord{
	virtual irr::xml::TagResult tagHandlerBefor(irr::xml::IFactoryEnvironment*environment,irr::xml::IXmlNode*node){
		irr::io::path href=node->getAttString("href");
		if (href==""){
			LOG("Can't CREATE Loadable");
			return irr::xml::STOP;
		}

		irr::scene::ISceneNode*parent=environment->getDevice()->getSceneManager()->getRootSceneNode();
		if ((node->getParent()) && node->getParent()->getTag())
			parent=(irr::scene::ISceneNode*)(node->getParent()->getTag());

		href=environment->getDevice()->getFileSystem()->getAbsolutePath(href);



		irr::scene::ISceneNodeLoadable*res=new irr::scene::ISceneNodeLoadable(environment->getDevice(),href,parent);
		if (node->indexOfAttribute("radius")>=0)
			res->setSize(node->getAttF32("radius",1),node->getAttVec3F("offset",0,0,0));
		if (node->indexOfAttribute("size")>=0)
			res->setSize(node->getAttVec3F("size",1,1,1),node->getAttVec3F("offset",0,0,0));
		res->setLoadDistation(node->getAttF32("distation",0));
		res->setAutoUpdateSize(node->getAttBool("updatesize",false));
		res->setSyncLoad(node->getAttBool("sync_load",true));
		readBaseSceneNodeTags(res,node);
		res->drop();
		node->setTag(res);
		LOG("CREATE Loadable");
		return irr::xml::NEXT;
	}

	virtual irr::core::array<irr::core::stringc> getProcessedTags(){
		irr::core::array<irr::core::stringc> res;
		res.push_back("loadable");
		return res;
	};
};

void regLoadable(irr::xml::IFactoryEnvironment*environment){
	LoadableFactoryRecord*temp=new LoadableFactoryRecord();
	environment->registerHandler(temp);
	temp->drop();
}
